/**
 * @file 后台接口文件
 *
 * @author fanxingguang
 * 2016-10-31
 */
'use strict';
const _ = require('lodash');
const modelColumn = require('../../models/column');
const DATA = {
    msg: '',
    code: 0,
    data: ''
};

/**
 * 将数据格式化成树
 *
 * @param  {Array}  data 只有标志parentId的数据
 * @return {Object} 树形数据
 */
const formatDataToTree = data => {
    let dataTree = [];
    let tmpData = JSON.parse(JSON.stringify(data));
    tmpData.forEach(item => {
        if (item.parentId === '-1') {
            dataTree.push(item);
        }
        else {
            // 利用所有对象都是引用，在这里即使已经被push到数组
            // 我们直接修改数据（添加children），还是会改变元数据
            let parentData = _.find(tmpData, {_id: item.parentId});
            if (parentData) {
                if (!parentData.children) {
                    parentData.children = [];
                }
                parentData.children.push(item);
            }
        }
    });
    return dataTree;
};



module.exports = {
    get: {
        getAllColumns(req, res) {
            let data = _.clone(DATA);
            modelColumn.find({}, {content: 0}, (err, result) => {
                data.data = result;
                res.json(data);
            });
        },
        getTopColumns(req, res) {
            let data = _.clone(DATA);
            modelColumn.find({parentId: '-1'}, {content: 0}, (err, result) => {
                data.data = result;
                res.json(data);
            });
        },
        getSubColumnsById(req, res) {
            let data = _.clone(DATA);
            let id = req.query.id;
            modelColumn.find({parentId: id}, (err, result) => {
                data.data = result;
                res.json(data);
            });
        },
        getColumnTreeByType(req, res) {
            let data = _.clone(DATA);
            let type = req.query.type;
            modelColumn.find({type: type}, {content: 0}, (err, result) => {
                data.data = formatDataToTree(result);
                res.json(data);
            });
        },
        getColumnTree(req, res) {
            let data = _.clone(DATA);
            modelColumn.find({}, {content: 0}, (err, result) => {
                data.data = formatDataToTree(result);
                res.json(data);
            });
        },
        getColumnById(req, res) {
            let data = _.clone(DATA);
            let id = req.query.id;
            modelColumn.findById(id, (err, result) => {
                if (!result.type) {
                    result.type = -1;
                }
                data.data = result;
                res.json(data);
            });
        }
    },
    post: {
        addColumn(req, res) {
            let data = _.clone(DATA);
            let entity = req.body;
            modelColumn.create(entity, err => {
                if (err) {
                    data.code = 1;
                    data.msg = err + '';
                }
                res.json(data);
            });
        },
        deleteColumn(req, res) {
            let data = _.clone(DATA);
            let id = req.body.id;
            modelColumn.remove({
                $or: [
                    {_id: id},
                    {parentId: id}
                ]
            }, err => {
                res.json(data);
            });
        },
        updateColumn(req, res) {
            let id = req.body._id;
            let newColumn = _.cloneDeep(req.body);
            delete newColumn._id;
            modelColumn.findByIdAndUpdate(id, {$set: newColumn}, err => {
                let data = _.clone(DATA);
                if (err) {
                    data.code = 1;
                    data.msg = err + '';
                }
                res.json(data);
            });
        }
    }
};
